package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.dto.ChatGroupSettingDTO;
import com.zenithmind.chat.pojo.vo.ChatGroupSettingVO;
import com.zenithmind.chat.service.ChatGroupSettingService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 群组设置控制器 - 遵循单一职责原则
 * 只负责群组设置的HTTP接口处理
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/group-settings")
@RequiredArgsConstructor
@Validated
@Tag(name = "群组设置管理", description = "群组设置相关接口")
public class ChatGroupSettingController {

    private final ChatGroupSettingService chatGroupSettingService;

    @GetMapping("/{groupId}")
    @Operation(summary = "查询群组设置", description = "根据群组ID查询群组设置信息")
    public Result<ChatGroupSettingVO> getGroupSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        ChatGroupSettingVO setting = chatGroupSettingService.getGroupSetting(groupId);
        return Result.success(setting);
    }

    @PutMapping
    @Operation(summary = "更新群组设置", description = "更新群组的设置信息")
    public Result<Boolean> updateGroupSetting(@Valid @RequestBody ChatGroupSettingDTO dto) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateGroupSetting(dto, userId);
        return Result.success(result);
    }

    @PostMapping("/{groupId}/default")
    @Operation(summary = "创建默认设置", description = "为群组创建默认设置")
    public Result<String> createDefaultSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String userId = UserContext.getUserId();
        String settingId = chatGroupSettingService.createDefaultSetting(groupId, userId);
        return Result.success(settingId);
    }

    @DeleteMapping("/{groupId}")
    @Operation(summary = "删除群组设置", description = "删除指定群组的设置")
    public Result<Boolean> deleteGroupSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.deleteGroupSetting(groupId, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/name")
    @Operation(summary = "更新群组名称", description = "更新群组名称")
    public Result<Boolean> updateGroupName(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "群组名称") @RequestParam @NotBlank String groupName) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateGroupName(groupId, groupName, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/description")
    @Operation(summary = "更新群组描述", description = "更新群组描述信息")
    public Result<Boolean> updateGroupDescription(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "群组描述") @RequestParam String description) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateGroupDescription(groupId, description, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/avatar")
    @Operation(summary = "更新群组头像", description = "更新群组头像")
    public Result<Boolean> updateGroupAvatar(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "头像URL") @RequestParam @NotBlank String avatar) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateGroupAvatar(groupId, avatar, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/notice")
    @Operation(summary = "更新群组公告", description = "更新群组公告信息")
    public Result<Boolean> updateGroupNotice(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "群组公告") @RequestParam String notice) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateGroupNotice(groupId, notice, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/join-verification")
    @Operation(summary = "设置加群验证", description = "设置是否需要加群验证")
    public Result<Boolean> updateJoinVerification(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "是否需要验证") @RequestParam Boolean joinVerification) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateJoinVerification(groupId, joinVerification, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/all-muted")
    @Operation(summary = "设置全员禁言", description = "设置群组全员禁言状态")
    public Result<Boolean> updateAllMuted(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "是否全员禁言") @RequestParam Boolean allMuted) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateAllMuted(groupId, allMuted, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/visibility")
    @Operation(summary = "设置群组可见性", description = "设置群组是否公开")
    public Result<Boolean> updateGroupVisibility(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "是否公开") @RequestParam Boolean isPublic) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateGroupVisibility(groupId, isPublic, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/member-invite")
    @Operation(summary = "设置成员邀请权限", description = "设置是否允许成员邀请他人")
    public Result<Boolean> updateMemberInvitePermission(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "是否允许成员邀请") @RequestParam Boolean allowMemberInvite) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateMemberInvitePermission(groupId, allowMemberInvite, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/max-members")
    @Operation(summary = "设置最大成员数", description = "设置群组最大成员数量")
    public Result<Boolean> updateMaxMembers(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "最大成员数") @RequestParam Integer maxMembers) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateMaxMembers(groupId, maxMembers, userId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/message-retention")
    @Operation(summary = "设置消息保留天数", description = "设置群组消息保留天数")
    public Result<Boolean> updateMessageRetentionDays(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "消息保留天数") @RequestParam Integer messageRetentionDays) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.updateMessageRetentionDays(groupId, messageRetentionDays, userId);
        return Result.success(result);
    }

    @PutMapping("/batch")
    @Operation(summary = "批量更新群组设置", description = "批量更新多个群组的设置")
    public Result<Boolean> batchUpdateGroupSettings(
            @RequestBody @NotEmpty List<String> groupIds,
            @Valid @RequestBody ChatGroupSettingDTO dto) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.batchUpdateGroupSettings(groupIds, dto, userId);
        return Result.success(result);
    }

    @PostMapping("/{groupId}/reset")
    @Operation(summary = "重置为默认设置", description = "将群组设置重置为默认值")
    public Result<Boolean> resetToDefault(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.resetToDefault(groupId, userId);
        return Result.success(result);
    }

    @PostMapping("/copy")
    @Operation(summary = "复制群组设置", description = "将一个群组的设置复制到另一个群组")
    public Result<Boolean> copyGroupSettings(
            @Parameter(description = "源群组ID") @RequestParam @NotBlank String sourceGroupId,
            @Parameter(description = "目标群组ID") @RequestParam @NotBlank String targetGroupId) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.copyGroupSettings(sourceGroupId, targetGroupId, userId);
        return Result.success(result);
    }

    @GetMapping("/{groupId}/check-permission")
    @Operation(summary = "检查设置权限", description = "检查用户是否有权限修改指定设置")
    public Result<Boolean> checkSettingPermission(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "设置类型") @RequestParam @NotBlank String settingType) {
        String userId = UserContext.getUserId();
        Boolean hasPermission = chatGroupSettingService.checkSettingPermission(groupId, userId, settingType);
        return Result.success(hasPermission);
    }

    @GetMapping("/{groupId}/history")
    @Operation(summary = "查询设置历史", description = "查询群组设置的变更历史")
    public Result<List<ChatGroupSettingVO>> getSettingHistory(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "20") Integer pageSize) {
        List<ChatGroupSettingVO> history = chatGroupSettingService.getSettingHistory(groupId, pageNum, pageSize);
        return Result.success(history);
    }

    @GetMapping("/{groupId}/export")
    @Operation(summary = "导出群组设置", description = "导出群组设置为文件")
    public Result<String> exportGroupSettings(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String userId = UserContext.getUserId();
        String exportPath = chatGroupSettingService.exportGroupSettings(groupId, userId);
        return Result.success(exportPath);
    }

    @PostMapping("/{groupId}/import")
    @Operation(summary = "导入群组设置", description = "从文件导入群组设置")
    public Result<Boolean> importGroupSettings(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "文件路径") @RequestParam @NotBlank String filePath) {
        String userId = UserContext.getUserId();
        Boolean result = chatGroupSettingService.importGroupSettings(groupId, filePath, userId);
        return Result.success(result);
    }
}
